package com.registration.controller;

import com.registration.Annotation.OperateLog;
import com.registration.bean.OrderBean;
import com.registration.bean.ScheduleBean;
import com.registration.service.QueueService;
import com.registration.util.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/v1/queue")
public class QueueController {

    @Autowired
    private QueueService queueService;

    /**
     * 获取可预约排班
     * @param dept_id 部门id
     * @param visit_date 预约日期
     * @return 可预约排班
     */
    @GetMapping("/available")
    @OperateLog("获取可预约排班")
    public JsonResponse getAvailableSchedules(
            @RequestParam Integer dept_id,
            @RequestParam Date visit_date) {
        List<ScheduleBean> schedules = queueService.getAvailableSchedules(dept_id, visit_date);
        JsonResponse jsonResponse = new JsonResponse();
        try {
            jsonResponse.setCode(0);
            jsonResponse.setMsg("获取可预约排班成功");
            jsonResponse.setData(schedules);
        } catch (Exception e) {
            jsonResponse.setCode(1);
            jsonResponse.setMsg("获取可预约排班失败");
            throw new RuntimeException(e);
        }
        return jsonResponse;
    }

    /**
     * 获取当前叫号信息
     * @param schedule_id 排班id
     * @return 当前叫号信息
     */
    @GetMapping("/info/{schedule_id}")
    @OperateLog("获取当前叫号信息")
    public JsonResponse getCurrentQueueInfo(@PathVariable Integer schedule_id) {
        ScheduleBean schedule = queueService.getCurrentQueueInfo(schedule_id);
        JsonResponse jsonResponse = new JsonResponse();
        try {
            jsonResponse.setCode(0);
            jsonResponse.setMsg("获取当前叫号信息成功");
            jsonResponse.setData(schedule);
        } catch (Exception e) {
            jsonResponse.setCode(1);
            jsonResponse.setMsg("获取当前叫号信息失败");
            throw new RuntimeException(e);
        }
        return jsonResponse;
    }

    /**
     * 获取医生当前排班信息
     * @param doctor_id 医生id
     * @return 排班信息
     */
    @GetMapping("/doctor/{doctor_id}")
    @OperateLog("获取医生当前排班信息")
    public JsonResponse getDoctorCurrentSchedule(@PathVariable Integer doctor_id) {
        List<ScheduleBean> schedule = queueService.getDoctorCurrentSchedule(doctor_id);
        JsonResponse jsonResponse = new JsonResponse();
        try {
            jsonResponse.setCode(0);
            jsonResponse.setMsg("获取医生的当前排班信息成功");
            jsonResponse.setData(schedule);
        } catch (Exception e) {
            jsonResponse.setCode(1);
            jsonResponse.setMsg("获取医生的当前排班信息失败");
            throw new RuntimeException(e);
        }
        return jsonResponse;
    }

    /**
     * 更新号源
     * @param schedule_id 号源id
     * @param change 更新的数量
     * @return 更新结果
     */
    @PostMapping("/updateQuota")
    @OperateLog("更新号源")
    public JsonResponse updateScheduleQuota(
            @RequestParam Integer schedule_id,
            @RequestParam int change) {
        boolean success = queueService.updateScheduleQuota(schedule_id, change);

        JsonResponse jsonResponse = new JsonResponse();
        try {
            jsonResponse.setCode(0);
            jsonResponse.setMsg("更新号源成功");
            jsonResponse.setData(success);
        } catch (Exception e) {
            jsonResponse.setCode(1);
            jsonResponse.setMsg("更新号源失败");
            throw new RuntimeException(e);
        }
        return jsonResponse;
    }




    /**
     * 根据就诊人ID获取排队叫号信息
     * @param patientId 就诊人ID
     * @return 排队叫号信息
     */
    @GetMapping("/patient/{patientId}")
    @OperateLog("根据就诊人ID获取排队叫号信息")
    public JsonResponse getQueueInfoByPatientId(@PathVariable Integer patientId) {
        List<ScheduleBean> schedules = queueService.getQueueInfoByPatientId(patientId);
        JsonResponse jsonResponse = new JsonResponse();
        try {
            jsonResponse.setCode(0);
            jsonResponse.setMsg("获取就诊人排队信息成功");
            jsonResponse.setData(schedules);
        } catch (Exception e) {
            jsonResponse.setCode(1);
            jsonResponse.setMsg("获取就诊人排队信息失败");
            throw new RuntimeException(e);
        }
        return jsonResponse;
    }



    /**
     * 获取就诊人排队信息
     * @param patientId 就诊人ID
     * @return 排队叫号信息
     */

    @GetMapping("/showQueue/{patientId}")
    public JsonResponse showQueue(@PathVariable Integer patientId) {
        List<OrderBean> schedules = queueService.showQueue(patientId);
        JsonResponse jsonResponse = new JsonResponse();
        try {
            jsonResponse.setCode(0);
            jsonResponse.setMsg("获取就诊人排队信息成功");
            jsonResponse.setData(schedules);
        } catch (Exception e) {
            jsonResponse.setCode(1);
            jsonResponse.setMsg("获取就诊人排队信息失败");
            throw new RuntimeException(e);
        }
        return jsonResponse;
    }


    /**
     * 根据排班ID获取排队叫号信息
     * @param scheduleId 排班ID
     * @return 排队叫号信息
     */
    @GetMapping("/showQueueAndMy/{scheduleId}")
    public JsonResponse showQueueAndMy(@PathVariable Integer scheduleId) {
        List<ScheduleBean> schedules = queueService.showQueueAndMy(scheduleId);
        JsonResponse jsonResponse = new JsonResponse();
        try {
            jsonResponse.setCode(0);
            jsonResponse.setMsg("获取就诊人排队信息成功");
            jsonResponse.setData(schedules);
        } catch (Exception e) {
            jsonResponse.setCode(1);
            jsonResponse.setMsg("获取就诊人排队信息失败");
        }
        return jsonResponse;
    }








}
